<?php

require_once('admincontroller.php');

class Contents extends AdminController
{
	function Contents()
	{
		parent::AdminController();
		$this->load->model('Content');
		$this->main_navigation = 'site';
		$this->sub_navigation = 'contents';
	}

	function index()
	{
		$data['contents'] = $this->Content->all();
		$this->display('content_list', $data);
	}

	// Page de création
	function new_content()
	{
		$data['content'] = new Content();
		$this->display('content_new', $data);
	}

	// Page d'édition
	function edit($id)
	{
		$data['content'] = $this->Content->get_by_id($id);
		$this->display('content_edit', $data);
	}

	// Création d'un content
	function create()
	{
		$content = new Content();
		$content->creator_id = $content->updater_id = $this->session->userdata('user_id');

		$errors = $this->Content->set_data($content, $_POST);

		if (count($errors) == 0 && $this->Content->save($content)) {
			$data['success_notice'] = 'Page créée !';
			$data['contents'] = $this->Content->all();
			$this->display('content_list', $data);
		}
		else {
			$data['error_notice'] = $errors;
			$data['content'] = $content;
			$this->display('content_new', $data);
		}
	}

	// Mise à jour d'un content
	function update($id)
	{
		$content = $this->Content->plain_get($id);
		$content->updater_id = $this->session->userdata('user_id');

		$errors = $this->Content->set_data($content, $_POST);

		if (count($errors) == 0 && $this->Content->save($content)) {
			$data['success_notice'] = 'Page mise à jour !';
			$data['contents'] = $this->Content->all();
			$this->display('content_list', $data);
		}
		else {
			$data['error_notice'] = $errors;
			$data['content'] = $content;
			$this->display('content_edit', $data);
		}
	}

	// Suppression d'un content
	function delete($id)
	{
		if (!$this->Content->delete($id)) {
			$this->output->set_status_header(400);
		}
	}

	function restore($id)
	{
		if ($this->Content->restore($id)) {
			$data['success_notice'] = 'Suppression de la page annulée';
		}
		else {
			$data['error_notice'] = 'Impossible de restaurer la page';
		}
		$data['contents'] = $this->Content->all();
		$this->display('content_list', $data);
	}

	// Détails d'un contenu
	function details($id)
	{
		$data['content'] = $this->Content->get_by_id($id);
		$this->load->view('admin/contents/content_details', $data);
	}

}
